package com.example.demo.mqserver.dao;

import com.example.demo.MqApplication;
import com.example.demo.mqserver.core.Binding;
import com.example.demo.mqserver.core.Exchange;
import com.example.demo.mqserver.core.ExchangeType;
import com.example.demo.mqserver.core.MSGQueue;
import com.example.demo.mqserver.mapper.MetaMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.List;

/**
 * 通过该类 , 实现对数据库的管理操作
 */
public class DataBaseManager {

    private MetaMapper mapper;

    // 初始化方法 --
    // ①构造方法一般用来初始化类的属性,但是一般不太会涉及到太多的业务逻辑
    // 此处的初始化带有业务逻辑,因为手动调用
    public void init() {
        // 数据库初始化,建库建表 + 插入默认数据
        // 希望在borker server启动的时候,做出下列逻辑
        // 如果数据库存在,不做任何操作
        // 如果数据库不存在,则建库建表,构造默认数据
        // 判定存在不存在,判定meta.db存在是否即可
        mapper = MqApplication.context.getBean(MetaMapper.class);// 获取mapper的bean对象
        if (!checkDBExists()) {
            // 数据库不存在 == 建库建表插入默认数据
            File dir = new File("./data");
            dir.mkdirs();
            createTable();
            createDefaultData();
            System.out.println("[DataBaseManager]数据库初始化完成!");
        } else {
            System.out.println("[DatabaseManager]数据库已存在!");
        }


    }


    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        if (file.exists()) {
            return true;
        }
        return false;
    }


    // 建库操作无需手动执行,因为sqlite在首次执行到这里的数据库操作的时候就会自动创建meta.db
    private void createTable() {
        mapper.createExchangeTable();
        mapper.createQueueTable();
        mapper.createBindingTable();
        System.out.println("建库建表完成!");
    }


    // 添加默认数据 -- 默认交换机(匿名直接DIRECT交换机 )
    private void createDefaultData() {
        // 构造一个默认的交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(true);
        mapper.insertExchange(exchange);
        System.out.println("创建初始数据完成!");
    }

    public void insertExchange(Exchange exchange) {
        mapper.insertExchange(exchange);
    }

    public void deleteExchange (String exchangeName) {
        mapper.deleteExchange(exchangeName);
    }

    public void insertQueue (MSGQueue msgQueue) {
        mapper.insertQueue(msgQueue);
    }

    public void deleteQueue (String queueName) {
        mapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding) {
        mapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        mapper.deleteBinding(binding);
    }

    public List<Exchange> selectAllExchanges () {
        return mapper.selectAllExchanges();
    }

    public List<MSGQueue> selectAllQueues () {
        return mapper.selectAllQueues();
    }

    public List<Binding> selectAllBinding () {
        return mapper.selectAllBindings();
    }


    public void deleteDB () {
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }

        File dir = new File("./data");
        boolean ret1 = dir.delete();
        if (ret1) {
            System.out.println("目录删除成功");
        } else {
            System.out.println("目录删除失败");
        }
    }





}
